package com.cloud.controller.api.login;

import com.alibaba.fastjson.JSON;
import com.channel.base.WxChatService;
import com.channel.wxpublic.token.AccessTokenData;
import com.channel.wxpublic.token.AccessTokenParam;
import com.channel.wxpublic.token.WxMpAccessToken;
import com.cloud.base.annotation.LinkedQuery;
import com.cloud.base.bean.OpenConfig;
import com.cloud.controller.data.login.*;
import com.cloud.controller.from.login.RequestLoginParam;
import com.cloud.service.user.UserService;
import com.common.utils.tools.jwt.JwtUtils;
import com.common.utils.tools.md5.Md5Utils;
import com.common.utils.tools.result.BaseReturn;
import com.common.utils.tools.type.jwt.SubjectType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/user")
public class LoginController {

    @Autowired
    UserService userService;

    @Autowired
    WxChatService wxChatService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    WxMpAccessToken wxMpAccessToken;






    @RequestMapping("/test")
    public Object test(){
        OpenConfig openConfig = new OpenConfig();
        openConfig.setAppId("wxd21ecc79b39fa803");
        openConfig.setSecret("49abeaa455f89a28b01cf78ad6716fc2");
        AccessTokenParam accessTokenParam = new AccessTokenParam();
        accessTokenParam.setAppid(openConfig.getAppId());
        accessTokenParam.setSecret(openConfig.getSecret());
        AccessTokenData accessToken1 = wxMpAccessToken.getAccessToken(accessTokenParam);
       /* String accessToken = wxChatService.getAccessToken(openConfig);
        System.out.println(accessToken);*/
        return accessToken1;
    }


    @RequestMapping(value = "/test1")
    @LinkedQuery
    public OrderData test1(){
        return buildTestOrder();
    }
    OrderData buildTestOrder(){
        OrderData orderData = new OrderData();
        orderData.setId(1L);
        orderData.setOrderNo("1000000000000000001");
        orderData.setUserId(1L);
        return orderData;
    }

    List<OrderData> buildTestOrders(){
        List<OrderData> orderData1 = new ArrayList<>();
        OrderData orderData = new OrderData();
        orderData.setId(1L);
        orderData.setOrderNo("1000000000000000001");
        orderData.setUserId(1L);
        OrderData orderData2 = new OrderData();
        orderData2.setId(2L);
        orderData2.setOrderNo("1000000000000000002");
        orderData2.setUserId(2L);
        orderData1.add(orderData);
        orderData1.add(orderData2);
        return orderData1;
    }

    @RequestMapping("/logout")
    public BaseReturn logout(@RequestParam(value = "token") String token){
        String authToken = stringRedisTemplate.opsForValue().get(token);
        if(StringUtils.isEmpty(authToken)){
           return BaseReturn.build("500","登录已过期请重新登录");
        }
        Boolean delete = stringRedisTemplate.opsForValue().getOperations().delete(token);
        if(delete != null){
           if(delete){
               return BaseReturn.build("200","退出成功");
           }else {
               return BaseReturn.build("500","操作失败");
           }
        }
        return BaseReturn.build("500","登录已过期请重新登录");
    }


    @RequestMapping("/login")
    public BaseReturn login(@RequestBody RequestLoginParam requestLoginParam){
        LoginData loginData = new LoginData();
        com.cloud.domain.user.User byAccountName = userService.getByAccountName(requestLoginParam.getUserName());
        if(byAccountName == null){
           return BaseReturn.build("500","该用户不存在");
        }
        String password = byAccountName.getPassword();
        boolean saltverifyMD5 = Md5Utils.getSaltverifyMD5(requestLoginParam.getPassword(), password);
        if(!saltverifyMD5){
            return BaseReturn.build("500","密码错误");
        }
        String token = JwtUtils.geneJsonWebToken(byAccountName.getId(), byAccountName.getUserName(), SubjectType.USER);
        stringRedisTemplate.opsForValue().set(token, JSON.toJSONString(loginData),60 * 30, TimeUnit.SECONDS);
        loginData.setAuthToken(token);
        return BaseReturn.build("200","登录成功",loginData);
    }
    @RequestMapping("/getInfo")
    public BaseReturn getInfo(String authToken) throws Exception {
        //String token = stringRedisTemplate.opsForValue().get(authToken);
        User user = new User();
        user.setAvatarUrl("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        user.setNikeName("土豆");
        Customer customer = new Customer();
        customer.setCustomerNo("0000001");
        UserInfoData userInfoData = new UserInfoData();
        userInfoData.setAuthToken("123456789..");
        userInfoData.setUser(user);
        return BaseReturn.build("200","登录成功",userInfoData);
    }


}
